//
//  YZMBuyCountView.m
//  YZMicroStock
//
//  Created by apple on 2018/1/16.
//  Copyright © 2018年 cqjr. All rights reserved.
//

#import "YZMBuyCountView.h"
#import "NSNumber+Utils.h"

@interface YZMBuyCountView ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textFieldCount;

@property (weak, nonatomic) IBOutlet UILabel *labelInfo;

@end

@implementation YZMBuyCountView{
    UIButton *_priBtn;
    double    _lastMaxCount;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)awakeFromNib{
    [super awakeFromNib];
    for (NSInteger i = 0; i < 104; i++) {
        UIButton *btn = [self viewWithTag:100+i];
        if (i == 0) {
            _priBtn = btn;
        }
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn setTitleColor:[YZMAppStyle buttonSelectedColor] forState:UIControlStateNormal];
        [btn setTitleColor:[YZMAppStyle colorForBlackText] forState:UIControlStateSelected];
        [btn setBackgroundImage:[UIImage createImageWithColor:[YZMAppStyle buttonSelectedColor]] forState:UIControlStateSelected];
        [YZMSetBorderAndCornerRadius setView:btn borderWidth:1.0 borderColor:[YZMAppStyle colorForYellowButton] cornerRadius:4.0];
        btn.layer.masksToBounds = YES;
        [btn addTarget:self action:@selector(clickedBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    self.backgroundColor = [YZMAppStyle colorForMain];
    self.textFieldCount.delegate = self;
    UIView *ivLeft = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 44)];
    self.textFieldCount.leftView = ivLeft;
    self.textFieldCount.leftViewMode = UITextFieldViewModeAlways;
    UIView *ivRight = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
    self.textFieldCount.rightView = ivRight;
    self.textFieldCount.rightViewMode = UITextFieldViewModeAlways;
    [YZMSetBorderAndCornerRadius setView:self.textFieldCount borderWidth:1.0 borderColor:[YZMAppStyle colorForLightGrayText] cornerRadius:4.0];
    self.labelInfo.textColor = [YZMAppStyle colorForLightGrayText];
//    [self.textFieldCount addTarget:self action:@selector(changeText:) forControlEvents:UIControlEventEditingChanged];
    UIView *ivAccess = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    ivAccess.backgroundColor = [YZMAppStyle colorForgray];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"完成" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(changeTextFinish:) forControlEvents:UIControlEventTouchUpInside];
    [ivAccess addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(ivAccess);
        make.top.bottom.equalTo(ivAccess);
        make.width.mas_equalTo(65);
    }];
    self.textFieldCount.inputAccessoryView = ivAccess;
}

- (void)clickedBtn:(UIButton *)btn{
    if ([btn isEqual:_priBtn]) {
        return;
    }
    switch (btn.tag) {
        case 100:
//            self.textFieldCount.text = @(self.maxCount).p0fString;
            [self change100Count:1.0 isMaxCount:YES];
            break;
        case 101:
//             self.textFieldCount.text = @(ceil([self.textFieldCount.text integerValue] / 2.0)).p0fString;
            [self change100Count:2.0 isMaxCount:YES];
            break;
        case 102:
            [self change100Count:3.0 isMaxCount:YES];
//            self.textFieldCount.text = @(ceil([self.textFieldCount.text integerValue] / 3.0)).p0fString;
            break;
        case 103:
            [self change100Count:4.0 isMaxCount:YES];
//            self.textFieldCount.text = @(ceil([self.textFieldCount.text integerValue] / 4.0)).p0fString;
            break;
        default:
            break;
    }
    _priBtn.selected = NO;
    btn.selected = YES;
    _priBtn = btn;
}

- (void)change100Count:(float)b isMaxCount:(BOOL)isMaxCount {
    
    NSString *str = [NSString string];
    NSInteger count = isMaxCount ? self.maxCount / b : [self.textFieldCount.text integerValue] / b;
    
    if (count < self.minCount) {
        count = self.minCount;
    }
    if (count > self.maxCount) {
        count = self.maxCount;
    }
  
//    if (b < 2) {
//       str = @(ceil([self.textFieldCount.text integerValue] / b)).p0fString;
//       count = [str integerValue];//1110
//        if (count < self.minCount) {
//            count = self.minCount;
//        }
//        if (count > self.maxCount) {
//            count = self.maxCount;
//        }
//    }

//    NSInteger wanPoint = count / 10000 % 10;
//    NSInteger qianPoint = count / 1000 % 10;
//    NSInteger baiPoint = count / 100 % 10;
//    NSInteger shiPoint = count / 10 % 10;
    
//    if (baiPoint > 0 && shiPoint > 0) {
//        baiPoint += 1;
//    }
    NSInteger Point = count / 100;
    if (count % 100) {
        Point ++;
    }
    NSInteger totalCount = Point * 100 < 100 ? 100 : Point * 100;

//    NSInteger totalCount = shiwanPoint * 100000 + wanPoint *10000 + qianPoint * 1000 + baiPoint * 100;
    str = @(totalCount).p0fString;
    NSLog(@"str:%@",str);
    self.textFieldCount.text = str;
    if (self.handle) {
        self.handle(self.textFieldCount.text);
    }
}

- (void)setMaxCount:(double)maxCount{
    
    if (_lastMaxCount > 1) {
        return;
    }
    _lastMaxCount = maxCount;

    _lastMaxCount = _maxCount = maxCount < 100 ? 100 : maxCount;
    self.textFieldCount.text = @(_maxCount).p0fString;
    if (self.handle) {
        self.handle(self.textFieldCount.text);
    }
}

- (void)changeTextFinish:(UIButton *)btn{
//    _priBtn.selected = NO;
//    UIButton *btnAll = [self viewWithTag:100];
//    btnAll.selected = YES;
    [self change100Count:1.0 isMaxCount:NO];
    [self.textFieldCount resignFirstResponder];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    _priBtn.selected = NO;
}
@end
